<?php
/**
 * Created by PhpStorm.
 * User: UsWork-02
 * Date: 2021/7/3
 * Time: 9:58
 */

namespace app\project\controller;


use app\common\controller\Common;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Plandetail extends Controller
{
    private $pro_id = 0;
    public function _initialize()
    {
        $swidthStyle = Session::get("style");
        $this->assign("swidthStyle",$swidthStyle);
        $id = empty($_GET['id'])?0: $_GET['id'];
        $this->assign("id",$id);

        $library = empty($_GET['library'])?0:$_GET['library'];
        $this->assign("library",$library);
    }

    /**
     * 年度计划进度
     */
    public function index(){
        $common = new Common();
        //查询年度计划
        $plan = $common->public_select("project_pro_plan",['id'=>$_GET['id']])->find();
        $this->assign("plan",$plan);

        //查询项目
        $title = $common->public_select("project_pro",['id'=>$plan['pro_id']])->value('title');
        $this->assign("title",$title);
        if(!empty($_GET['keyword'])){
            $keyword = $_GET['keyword'];
            $where['title'] = ['like',"%{$keyword}%"];
        }
        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 20 : $_GET['limit'];
                $start = ($page - 1) * $limit;
//                $where['plan_id'] = $_GET['id'];
                $where['year'] = $plan['plan_title'];
                $where['pro_id'] = $plan['pro_id'];
                $list = $common->public_select("project_pro_plandetail",$where,null,$start,$limit)->order("month asc")->select();
                foreach ($list as $k=>$v){
                    //查询类别
                    $advance = '';
                    $advanceWhere = [];
                    if(!empty($v['advance_id'])){
                        $advanceWhere['id'] = $v['advance_id'];
                        $advance = $common->public_select("project_advance",$advanceWhere)->value("name");
                    }
                    $list[$k]['advance'] = empty($advance)?"/":$advance;
                    $list[$k]['apply'] = empty($v['declare'])?"否":"是";
                }
                $count = $common->public_select("project_pro_plandetail",$where)->count();
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        return $this->fetch("index");
    }

    /**
     * 年度计划进度视图
     */
    public function detail(){
        $common = new Common();
        if(!empty($_GET['id'])){
            $id = $_GET['id'];
            $detail = $common->public_select("project_pro_plandetail",['id'=>$id])->find();
//            $detail['images'] = explode(";",$detail['images']);
            $this->assign('detail',$detail);
        }
        //获取推进类别
        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];
        $where['status'] = 1;
        $advance = $common->public_select("project_advance",$where,"id,name")->select();
        $this->assign('advance',$advance);
        $template = empty($_GET['id'])?"addView":"saveView";
        return $this->fetch($template);
    }


    /**
     * 添加
     * @return mixed
     */
    public function add(){
        $thisWechat = Session::get("thisWechat");
        $data = $this->request->post();
        $common = new Common();
        try{
            $where['id'] = $data['plan_id'];
            //查询年份
            $plan = $common->public_select("project_pro_plan",$where,'pro_id,plan_title')->find();
            $data['year'] = $plan['plan_title'];
            $data['pro_id'] = $plan['pro_id'];

            $this->condition($data);
            $data['addtime'] = date("Y-m-d h:i:s",time());
            $data['add_id'] = Session::get("id");
            $common->handle("project_pro_plandetail",$data,1);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 修改
     * @return mixed
     */
    public function save(){
        $thisWechat = Session::get("thisWechat");
        $data = $this->request->post();
        $common = new Common();
        try{
            $where['id'] = $data['plan_id'];
            //查询年份
            $plan = $common->public_select("project_pro_plan",$where,'pro_id,plan_title')->find();
            $data['year'] = $plan['plan_title'];
            $data['pro_id'] = $plan['pro_id'];
            $this->condition($data);
            $data['updatetime'] = date("Y-m-d h:i:s",time());
            $data['update_id'] = Session::get("id");
            $common->handle("project_pro_plandetail",$data,1);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 删除
     */
    public function del(){
        try{
            $common = new Common();
            $id = $_POST['id'];
            $res = $common->public_select("project_pro_plandetail",['id'=>['in',$id]])->delete();
            if(empty($res)){
                throw new Exception("删除失败");
            }
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    /**
     * @param $data
     */
    public function condition($data){
        $rule = [
            ["month", "require", "月份不能为空"],
            ["content", "require", "节点计划不能为空"],
        ];
        $validate = new Validate($rule);
        if (!$validate->check($data)) {
            throw new Exception($validate->getError());
        }
        if($data['month']>12 | $data['month']<1){
            throw new Exception("月份有误");
        }

        //判断该月份是否已有数据
        $common = new Common();
        $where['pro_id'] = $data['pro_id'];
        $where['year'] = $data['year'];
        $where['month'] = $data['month'];
        if(!empty($data['id'])){
            $where['id'] = ['neq',$data['id']];
        }
        $detail = $common->public_select("project_pro_plandetail",$where)->find();
        if(!empty($detail)){
            throw new Exception($data['month']."月已存在节点计划，请勿重复添加");
        }

    }


    //申报操作
    public function report(){
        $common = new Common();
        $id = $_GET['id'];
        $detail = $common->public_select("project_pro_plandetail",['id'=>$id])->find();

        if(!empty($detail['images'])){
            $detail['images'] = explode(";",$detail['images']);
        }
        $this->assign('detail',$detail);

        return $this->fetch("report");
    }

    /**
     * 修改
     * @return mixed
     */
    public function doreport(){
        $thisWechat = Session::get("thisWechat");
        $data = $this->request->post();
        $common = new Common();
        try{
            $rule = [
                ["declareTime", "require", "时间不能为空"],
                ["declare", "require", "内容不能为空"],
                ["images", "require", "图片不能为空"],
            ];
            $validate = new Validate($rule);
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            if(!empty($data['images'])){
                $data['images'] = implode(';',$data['images']);
            }
            $data['actualTime'] = time();
            $data['updatetime'] = date("Y-m-d h:i:s",time());
            $data['update_id'] = Session::get("id");
            $common->handle("project_pro_plandetail",$data,1);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }
}